package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 预约列表
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;
    //套餐信息查询
    @RequestMapping("/findSetmeal")
    public Result findSetmeal(){
        try {
            List<Setmeal> list = orderService.findSetmeal();
            return new Result(true,"查询套餐列表成功",list);
        }catch (Exception e){
            return new Result(false,"查询地址列表失败");
        }
    }
    //新增预约
    @RequestMapping("/addOrder")
    public Result addOrder(@RequestBody Map map, Integer[] setmealIds) throws Exception {
        Result result = null;
        map.put("orderType", Order.ORDERTYPE_TELEPHONE);
        for (Integer setmealId : setmealIds) {
            map.put("setmealId", String.valueOf(setmealId));
            result = orderService.orders(map);
        }
        return result;
    }

    //查询预约信息列表
    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){

        return orderService.findAll(queryPageBean);
    }

    //确定预约
    @RequestMapping("/updateById")
    public Result updateById(Integer id){
        try {
            orderService.updateById(id);
            return  new Result(true, MessageConstant.CONFIRM_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.CONFIRM_ORDER_FAIL);
        }
    }

    //删除预约
    @RequestMapping("/cancelById")
    public Result cancelById(Integer id) {
        try {
            orderService.cancelById(id);
            return new Result(true, MessageConstant.CANCEL_ORDER_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.CANCEL_ORDER_FALL);
        }
    }


}
